﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AgileNYCWebService.Manager.Crypter
{
	internal class Crypter
	{
		private const string InitVector = "tu89geji340t89u2";

		// This constant is used to determine the keysize of the encryption algorithm.
		private const int Keysize = 256;

		private const string PasswordPhrase = "Test";

		public static string Encrypt(string plainText)
		{
			try
			{
				byte[] initVectorBytes = Encoding.UTF8.GetBytes(InitVector);
				byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
				PasswordDeriveBytes password = new PasswordDeriveBytes(PasswordPhrase, null);
				byte[] keyBytes = password.GetBytes(Keysize / 8);
				RijndaelManaged symmetricKey = new RijndaelManaged();
				symmetricKey.Mode = CipherMode.CBC;
				ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
				cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
				cryptoStream.FlushFinalBlock();
				byte[] cipherTextBytes = memoryStream.ToArray();
				memoryStream.Close();
				cryptoStream.Close();
				return Convert.ToBase64String(cipherTextBytes);
			}
			catch (Exception)
			{
				throw;
			}
		}

		public static string Decrypt(string cipherText)
		{
			try
			{
				byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);
				byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
				PasswordDeriveBytes password = new PasswordDeriveBytes(PasswordPhrase, null);
				byte[] keyBytes = password.GetBytes(Keysize / 8);
				RijndaelManaged symmetricKey = new RijndaelManaged();
				symmetricKey.Mode = CipherMode.CBC;
				ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
				MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
				CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
				byte[] plainTextBytes = new byte[cipherTextBytes.Length];
				int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
				memoryStream.Close();
				cryptoStream.Close();
				return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}